<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin extends CI_Controller {

    function __construct() {
        session_start();
        parent::__construct();

        $this->load->database();
        $this->load->helper('url');

        $this->load->library('grocery_CRUD');
    }

    function _example_output($output = null) {
        if (!isset($_SESSION['username'])) {
            redirect('login');
        }
        $session['session_type'] = $_SESSION['type'];

        $this->load->view('admin/header_in', $session);
        $this->load->view('admin/main_view.php', $output);
        $this->load->view('admin/footer');
    }

    function vehiculos() {
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $crud->set_table('vehiculos');
        $crud->required_fields('categoria', 'categoria', 'marca', 'modelo', 'transmision', 'combustible', 'anno', 'cilindrada', 'kilometraje', 'color_exterior', 'precio', 'foto1');
        /* $crud->display_as('color_ojos', 'Ojos');
          $crud->display_as('color_piel', 'Piel');
          $crud->display_as('color_pelo', 'Cabello');
          $crud->set_relation('edad', 'edades', 'edad');
          $crud->set_relation('estatura', 'estaturas', 'estatura');
          $crud->set_relation('color_ojos', 'color_ojos', 'color_ojos');
          $crud->set_relation('color_pelo', 'color_pelo', 'color_pelo');
          $crud->set_relation('color_piel', 'color_piel', 'color_piel');
          $crud->set_relation('provincia', 'provincias', 'provincia'); */
        $crud->display_as('nuevo', 'Aparecer en nuevos');
        $crud->display_as('oferta', 'Aparecer en ofertas');
        $crud->set_relation('categoria', 'categorias', 'categoria');
        $crud->set_relation('color_interior', 'color_interior', 'color_interior', null, 'color_interior ASC');
        $crud->set_relation('color_exterior', 'color_exterior', 'color_exterior', null, 'color_exterior ASC');
        $crud->set_relation('anno', 'anno', 'anno', null, 'id DESC');
        $crud->set_relation('marca', 'marcas', 'marca');
        $crud->set_relation('tipo', 'tipo', 'tipo');
        $crud->set_relation('combustible', 'combustibles', 'combustible');
        $crud->set_relation('cilindrada', 'cilindrada', 'cilindrada', null, 'id ASC');
        $crud->set_relation('transmision', 'transmision', 'transmision');
        $crud->set_relation('precio', 'precios', 'precio', null, 'id ASC');
        $crud->set_relation_n_n('Extras', 'vehiculo_extras', 'extras', 'vehiculo_id', 'extra_id', 'extra');
        $crud->columns('marca', 'modelo', 'anno', 'categoria', 'foto1');
        $crud->set_field_upload('foto1', 'assets/uploads/files');
        $crud->set_field_upload('foto2', 'assets/uploads/files');
        $crud->set_field_upload('foto3', 'assets/uploads/files');
        $crud->set_field_upload('foto4', 'assets/uploads/files');
        $crud->set_field_upload('foto5', 'assets/uploads/files');
        $crud->set_field_upload('foto6', 'assets/uploads/files');
        $crud->set_field_upload('foto7', 'assets/uploads/files');
        $crud->set_field_upload('foto8', 'assets/uploads/files');
        $crud->set_field_upload('foto9', 'assets/uploads/files');
        $crud->set_field_upload('foto10', 'assets/uploads/files');
        //$crud->callback_after_upload(array($this, 'image_uploader'));
        $crud->callback_after_upload(array($this, 'image_uploader_watermark'));
        $output = $crud->render();

        $this->_example_output($output);
    }

    function encrypt_password($post_array, $primary_key = null) {

        $this->load->helper('security');
        $post_array['password'] = do_hash($post_array['password'], 'md5');
        return $post_array;
    }

    function users() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('users');
            $crud->set_subject('Usuarios');
            $crud->required_fields('username', 'password');
            $crud->change_field_type('password', 'password');
            $crud->callback_before_insert(array($this, 'encrypt_password'));
            $crud->callback_before_update(array($this, 'encrypt_password'));

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function categorias() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('categorias');
            $crud->set_subject('Categoria');
            $crud->required_fields('categoria');

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function color_interior() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('color_interior');
            $crud->set_subject('Color interior');
            $crud->required_fields('color_interior');

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function color_exterior() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('color_exterior');
            $crud->set_subject('Color exterior');
            $crud->required_fields('color_exterior');

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function anno() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('anno');
            $crud->set_subject('Ano');
            $crud->required_fields('anno');

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function marcas() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('marcas');
            $crud->set_subject('Marca');
            $crud->required_fields('marcas');

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function tipo() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('tipo');
            $crud->set_subject('Tipo');
            $crud->required_fields('tipo');

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function combustibles() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('combustibles');
            $crud->set_subject('Combustible');
            $crud->required_fields('combustible');

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function cilindrada() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('cilindrada');
            $crud->set_subject('Cilindrada');
            $crud->required_fields('cilindrada');

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function transmision() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('transmision');
            $crud->set_subject('Transmision');
            $crud->required_fields('transmision');

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function extras() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('extras');
            $crud->set_subject('Extras');
            $crud->required_fields('extra');

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function precios() {
        try {
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');
            $crud->set_table('precios');
            $crud->set_subject('Precios');
            $crud->required_fields('precio');

            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    /* function listaModelos() {
      $crud = new grocery_CRUD();

      $crud->set_theme('datatables');
      $crud->set_table('vehiculos');
      $crud->set_subject('Modelos');
      $crud->columns('nombre', 'edad', 'estatura', 'provincia', 'foto1');

      $crud->set_field_upload('foto1', 'assets/uploads/files');
      $crud->set_field_upload('foto2', 'assets/uploads/files');
      $crud->set_field_upload('foto3', 'assets/uploads/files');
      $crud->set_field_upload('foto4', 'assets/uploads/files');
      $crud->set_field_upload('foto5', 'assets/uploads/files');
      $crud->set_field_upload('foto6', 'assets/uploads/files');
      $crud->set_field_upload('foto7', 'assets/uploads/files');
      $crud->set_field_upload('foto8', 'assets/uploads/files');
      $crud->set_field_upload('foto9', 'assets/uploads/files');
      $crud->set_field_upload('foto10', 'assets/uploads/files');
      $crud->callback_after_upload(array($this, 'image_uploader'));



      $output = $crud->render();

      $this->_example_output($output);
      } */

    function image_uploader($uploader_response, $field_info, $files_to_upload) {
        $this->load->library('image_moo');

        //Is only one file uploaded so it ok to use it with $uploader_response[0].
        $file_uploaded = $field_info->upload_path . '/' . $uploader_response[0]->name;

        $this->image_moo->load($file_uploaded)->resize(442, 342)->save($file_uploaded, true);

        return true;
    }

    function image_uploader_watermark($uploader_response, $field_info, $files_to_upload) {

        $this->load->library('image_moo');

        //Is only one file uploaded so it ok to use it with $uploader_response[0].
        $file_uploaded = $field_info->upload_path . '/' . $uploader_response[0]->name;
        /* $this->image_moo
          ->load($file_uploaded)
          ->make_watermark_text("MatMoo.com", "arial.ttf", 25, "#00ff00")
          ->resize(400, 400)
          ->watermark(2)
          ->save($file_uploaded, true); */
        $this->image_moo
                ->load($file_uploaded)
                ->load_watermark("watermark.png")
                ->resize(1024, 768)
                ->watermark(2)
                ->save($file_uploaded, true);

        return true;
    }

    public function index() {

        if (!isset($_SESSION['username'])) {
            redirect('login');
        }
        $session['session_type'] = $_SESSION['type'];

        $this->vehiculos();
    }

    public function logout() {
        session_destroy();
        redirect('login');
    }

}